#include<sys/types.h>
#include<sys/stat.h>
#include<iostream>
#include<cstring>
#include<unistd.h>
#include<cstdlib>
#include<fcntl.h>

#define MODE 0666

//管理管道
int main()
{
  //创建信道
  int n = mkfifo("pfifo", MODE);
  if (n == 1)
  {
    perror("mkfifo make error");
    exit(-1); 
  }
  
  //打开信道
  
  int fd = open("pfifo", O_RDONLY);
  if (fd < 0)
  {
    perror("open name pipes error");
    exit(-3);
  }

  //开始通信

  while (true)
  {
    char buffer[1024] = {'\0'};
    int x = read(fd, buffer, sizeof(buffer));

    if (x > 0)
    {
      buffer[x] = '\0';
      std::cout << "client:   " << buffer << std::endl;
    }
    else if (x == 0)
    {
      break;
    }
  }


  //取消信道
  int m = unlink("pfifo");
  if (m == -1)
  {
    perror("unlink: erase error");
    exit(-2);
  }
  return 0;
}
